import requests
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import db
import mydate
from env import cookies
# 2013年1月1号开始
baseUrl = 'http://www.cwl.gov.cn'

startYear = 2020
sqlStr = ''
#程序中断一次 2018-07-31 
while startYear <= 2022:
    print(f'year:{startYear}')
    s1, e1, s2, e2 = mydate.getYearMonthDay(startYear)
    yearCount = 0
    while yearCount < 2:
        #dateUrl = f'{baseUrl}/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart=2019-07-01&dayEnd=2019-12-31'
        dateUrl = f'{baseUrl}/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart={s1}&dayEnd={e1}'
        if yearCount == 1:
            dateUrl = f'{baseUrl}/cwl_admin/front/cwlkj/search/kjxx/findDrawNotice?name=ssq&issueCount=&issueStart=&issueEnd=&dayStart={s2}&dayEnd={e2}'
        yearCount += 1
        date_res = requests.get(dateUrl).json()
        results = date_res['result']
        results.sort(key=lambda x: x['code'])
        for item in results:
            #detailUrl = baseUrl+'/c/2022/03/27/500071.shtml'
            detailUrl = f"{baseUrl}{item['detailsLink']}"
            print(f'#detailUrl:{detailUrl}')
            option = webdriver.ChromeOptions()
            s = Service(
                executable_path=r'D:\pf\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe')
            driver = webdriver.Chrome(service=s)
            driver.get(detailUrl)
            for cookie in cookies:
                driver.add_cookie(cookie)
            driver.refresh()
            soup = BeautifulSoup(driver.page_source, 'html.parser')
            qius = soup.select(".qiu-item.qiu-item-big")
            qiu7 = qius[:7]
            drawDate = item['date'][:10]
            print(f'1:{qiu7[0].text}')
            print(f'2:{qiu7[1].text}')
            print(f'3:{qiu7[2].text}')
            print(f'4:{qiu7[3].text}')
            print(f'5:{qiu7[4].text}')
            print(f'6:{qiu7[5].text}')
            print(f'7:{qiu7[6].text}')
            sqlTemp = f"INSERT into cp.WinningNumbers (type,Period,DrawDate,Nunmber1,Nunmber2,Nunmber3,Nunmber4,Nunmber5,Nunmber6,Nunmber7) VALUES(1,'{item['code']}','{drawDate}','{qiu7[0].text.strip()}','{qiu7[1].text.strip()}','{qiu7[2].text.strip()}','{qiu7[3].text.strip()}','{qiu7[4].text.strip()}','{qiu7[5].text.strip()}','{qiu7[6].text.strip()}'); "
            driver.quit()
            #sqlStr += sqlTemp
            db.insertData(sqlTemp)
            # time.sleep(random.randint(0,3))

    startYear += 1

